home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 2000
/
MacHack 2000.toast
/
pc
/
The Hacks
/
FinderGrok
/
Grok
/
Source
/
SelectFile.cp
< prev
next >
Wrap
Text File
|
2000-06-23
|
5KB
|
264 lines
//
// C++ Toolbox Stationery
// by Josef W. Wankerl
// 04/11/00
//
#include <Dialogs.h>
#include <Fonts.h>
#include <MacWindows.h>
#include <Menus.h>
#include <QuickDraw.h>
#include <TextEdit.h>
#include <string.h>
#include <ctype.h>
#include "SelectFile.h"
SelectFile::SelectFile()
{
editrec = NULL;
TheString = NULL;
Initialize();
}
void SelectFile::Initialize()
{
/* InitGraf(&qd.thePort);
InitFonts();
InitFloatingWindows();
InitMenus();
TEInit();
InitDialogs(nil);
InitCursor();
*/
}
void SelectFile::MyEventLoop()
{
RgnHandle cursorRgn = NewRgn();
Boolean gotEvent = false;
EventRecord event;
bool Stop = false;
TEActivate(editrec);
while( !Stop )
{
gotEvent = WaitNextEvent(everyEvent, &event, 1, cursorRgn);
if(gotEvent)
{
switch(event.what)
{
case updateEvt:
Stop = DoUpdate(&event);
break;
case keyDown:
case autoKey:
Stop = DoKeyDown(&event);
break;
case mouseDown:
Stop = DoMouseDown(&event);
break;
default:
Stop = DoIdle(&event);
// do idle
}; // case
};
};
TEDeactivate(editrec);
};
void SelectFile::Run()
{
WindowPtr myWindow;
Rect theRect;
OSStatus err;
SetRect( &theRect, 40, 40, 400, 80);
// create the main window.
err = CreateNewWindow(kFloatingWindowClass, kWindowStandardFloatingAttributes, &theRect, &myWindow);
err = TransitionWindow(myWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, &theRect);
ShowFloatingWindows();
SetPort(myWindow);
// create the Text Edit
SetRect( &theRect, 0, 0, 360, 40);
editrec = TENew(&theRect, &theRect);
// Loop 'till your hearts content
MyEventLoop();
// clean up
TEDispose(editrec);
CloseWindow(myWindow);
DisposeWindow(myWindow);
}
bool SelectFile::DoUpdate(EventRecord* event)
{
Rect theRect;
theRect.top = 0;
theRect.left = 0;
theRect.bottom = 40;
theRect.right = 360;
BeginUpdate((WindowPtr)event->message);
SetPort((WindowPtr)event->message);
EraseRect(&theRect);
TEUpdate(&theRect, editrec);
EndUpdate((WindowPtr)event->message);
return false;
} // DoUpdate
bool SelectFile::DoKeyDown(EventRecord* event)
{
if( (char)event->message != '\r')
{
TEKey((char)event->message, editrec);
return false;
}
else
{
CharsHandle hTextString = TEGetText(editrec);
unsigned char state = HGetState(hTextString);
HLock(hTextString);
unsigned long size = (*editrec)->teLength;
if(TheString != NULL) DisposePtr(TheString);
TheString = NewPtr(size + 1);
strncpy(TheString, *hTextString, size);
TheString[size] = '\0';
HSetState(hTextString, state);
return true;
}
} // DoKeyDown
bool SelectFile::DoMouseDown(EventRecord* event)
{
TEClick((Point)event->where,
((event->modifiers & shiftKey)!=0),
editrec);
return false;
} // DoMouseDown
bool SelectFile::DoIdle(EventRecord* /*event*/)
{
TEIdle(editrec);
return false;
} // DoIdle
bool SelectFile::PaternMatch(char* FileName)
{
// just call the aux version
return PaternMatchAux(FileName, TheString);
} // PaternMatch
bool SelectFile::PaternMatch(Str255 FileName)
{
// find the length of the pstring and create
// space in the heap for a copy
char SZ = FileName[0];
char* cFN = new char [ (unsigned long)(SZ+1) ];
// copy the memory and set the terminator to \0
memcpy(cFN, &FileName[1], SZ);
cFN[SZ] = '\0';
// cal the ascz verson
bool lval = PaternMatch( cFN );
// clean up and return
delete cFN;
return lval;
} // PaternMatch pascal versin
bool SelectFile::PaternMatchAux(char* FileName, char* Pat)
{
ThreeState Match = maby;
// loop through each string
while( Match == maby )
{
switch(*Pat)
{
case '?':
// match one of any char.
Pat++;
if( *FileName != '\0')
FileName++;
else
Match = no;
break;
case '*':
// match 0 or more any char
while((*Pat == '*') || (*Pat == '?'))
{
if( (*Pat == '?' ) && (*FileName != '\0') )
FileName++;
Pat++;
}
while( (*FileName != '\0') && (tolower(*FileName) != tolower(*Pat)) )
++FileName;
Match = ( PaternMatchAux(FileName, Pat) ? yes : no );
while( (Match != yes) && (*FileName != '\0') )
Match = ( PaternMatchAux(++FileName, Pat) ? yes : no );
break;
case '\0':
// we've reached the end of the pattern
Match = ((tolower(*FileName) == tolower(*Pat)) ? yes : no);
break;
default:
// this is a normal char
if( tolower(*FileName) == tolower(*Pat) )
{
FileName++;
Pat++;
}
else
Match = no;
}; // switch
}; // while
return( Match == yes );
}; // PaternMatch
#ifdef NEVER
void main(void)
{
SelectFile theApplication;
theApplication.Run();
bool test = theApplication.PaternMatch("\pbob.txt");
test = !test;
// shane, theApplication.TheString contains the string entered by the user.
}
#endif